package novel.mzx.com.fragment;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.View;
import android.widget.ImageView;

import com.gyf.immersionbar.ImmersionBar;
import com.lzy.okgo.OkGo;
import com.lzy.okgo.callback.StringCallback;
import com.lzy.okgo.model.Response;
import com.zhy.adapter.recyclerview.MultiItemTypeAdapter;

import java.util.List;

import butterknife.BindView;
import novel.mzx.com.R;
import novel.mzx.com.adapter.VideoAdapter;
import novel.mzx.com.base.BaseFragment;
import novel.mzx.com.bean.VideoBean;
import novel.mzx.com.http.Api;
import novel.mzx.com.utils.JSONUtils;
import novel.mzx.com.utils.ViewUtils;


public class VideoFragment extends BaseFragment {

    @BindView(R.id.recyclerView)
    RecyclerView recyclerView;


    private VideoAdapter videoAdapter;
    private GridLayoutManager mLinearLayoutManager;
    String type;

    @Override
    public int getLayoutResID() {

        return R.layout.fragment_video;
    }

    @Override
    public void initView(View view) {
        super.initView(view);
        ImmersionBar.with(this).titleBar(R.id.toolbar).keyboardEnable(true).init();
        ImmersionBar.with(this).keyboardEnable(false).statusBarDarkFont(true, 0.2f).navigationBarColor(R.color.btn3).init();

        mLinearLayoutManager = new GridLayoutManager(getActivity(), 1);
        recyclerView.setLayoutManager(mLinearLayoutManager);

        getDatas();
        registerBoradcastReceiver();
    }

    private void getDatas() {
        ViewUtils.cancelLoadingDialog();
        OkGo.<String>post(Api.mainHost+ Api.listByTypeVideoInfoUrl)
                .params("type",1)
                .execute(new StringCallback() {
                    @Override
                    public void onSuccess(Response<String> response) {
                        Log.e("课件列表",response.body());
                        VideoBean videoBean = JSONUtils.parserObject(response.body(),VideoBean.class);
                        int code = videoBean.getResultCode();
                        if(code == 200){
                            ViewUtils.cancelLoadingDialog();
                            List<VideoBean.Data> data = videoBean.getData();

                            videoAdapter = new VideoAdapter(getActivity(),R.layout.list_item_video,data);

                            recyclerView.setAdapter(videoAdapter);


                        }
                    }

                    @Override
                    public void onError(Response<String> response) {
                        super.onError(response);

                    }
                });
    }

    public void registerBoradcastReceiver() {
        IntentFilter myIntentFilter = new IntentFilter();
        myIntentFilter.addAction("freshFr");
        //注册广播
        getActivity().registerReceiver(broadcastReceiver, myIntentFilter);
    }


    private BroadcastReceiver broadcastReceiver = new BroadcastReceiver() {

        @Override
        public void onReceive(Context context, Intent intent) {

        }
    };






}
